#include "citysetdlg.h"

CitySetDlg::CitySetDlg(QWidget *parent) : QWidget(parent)
{

    this->setGeometry(QRect(300, 100, 600, 300));

    this->setMinimumSize(600,300);
    this->setMaximumSize(600,300);
    this->setWindowFlags(Qt::FramelessWindowHint);//去掉标题栏

    label_pic_bg = new QLabel(this);
   // label_pic_bg->setGeometry(QRect(300, 150, 600, 400));
    label_pic_bg->setPixmap(QPixmap(":/images/set_bg.png"));
    label_pic_bg->setScaledContents(true);

    //显示按钮
    ok = new Button(this);
    ok->setButtonPicture(QPixmap( ":/images/ok_1.png"));
    ok->setPressPicture(QPixmap( ":/images/ok_2.png"));
    ok->setReleasePicture(QPixmap( ":/images/ok_1.png"));
    ok->setEnterPicture(QPixmap( ":/images/ok_2.png"));
    ok->setReleasePicture(QPixmap( ":/images/ok_1.png"));
    ok->set_X_Y_width_height(150,230,120,60);
    connect(ok,SIGNAL(clicked()),this,SLOT(goSet()) );


    cancel = new Button(this);
    cancel->setButtonPicture(QPixmap( ":/images/cancel_1.png"));
    cancel->setPressPicture(QPixmap( ":/images/cancel_2.png"));
    cancel->setReleasePicture(QPixmap( ":/images/cancel_1.png"));
    cancel->setEnterPicture(QPixmap( ":/images/cancel_2.png"));
    cancel->setReleasePicture(QPixmap( ":/images/cancel_1.png"));
    cancel->set_X_Y_width_height(330,230,120,60);
    connect(cancel,SIGNAL(clicked()),this,SLOT(close()) );

    keyNum = new KeyNum(this);
    keyNum->setItem("城市设置(输入编号):");
    keyNum->setGeometry(50,30,500,200);



    /*
    101010100,北京
    101020100,上海,上海,上海
    101030100,天津,天津,天津
    101200101,武汉,武汉,湖北
 */
    QString cityName[CODE_NUM] = {tr("北京"),tr("上海"),tr("天津"),tr("武汉")};
    QString cityCode[CODE_NUM] = {tr("101010100"),tr("101020100"),tr("101030100"),tr("101200101")};


    for(int i=0;i<CODE_NUM;i++){
        codeList[i].name = cityName[i];
        codeList[i].code = cityCode[i];
    }


}

void CitySetDlg::setItem(ShowLabel *item,QString *code)
{
   inputItem = item;
   inputCode = code;
}


int CitySetDlg::writeCode(QString code)
{
    QFile f("/home/fa/code_file");
    if(!f.open(QIODevice::WriteOnly | QIODevice::Text))
    {
        qDebug() << "Open failed." << endl;
        return -1;
    }

    QTextStream fStream(&f);
    fStream << code;
    f.close();
    return 0;
}

void CitySetDlg::goSet()
{
    QString code = keyNum->getValue();
    QString cityName;
    QString cityCode;
    bool findCode = false;

    for(int i=0;i<CODE_NUM;i++){

        if(codeList[i].code == code){
            cityName = codeList[i].name;
            cityCode = codeList[i].code;
            findCode = true;
            break;
        }
    }

    if(findCode)
    {
       // keyNum->showRet(tr("设置成功!"));
        qDebug() << keyNum->getValue();
        inputItem->setText( cityName);
        *inputCode=cityCode;
        //open
        //write cityCode
        //writeCode(cityCode);
        close();
    }else{
        keyNum->showRet(tr("设置无效!"));
    }

}
